// pages/live/live.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    liveId: '',
    playF: '',
    playR: '',
    userId: '',
    liveurl: '',
    videoContext: {}
  },
  backto: function () {
    console.log('返回')
  }
  ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('=====live  onLoad start ====================');
    var playF = '';
    var liveId = '';
    if (options.q !== undefined) {
      var scan_url = decodeURIComponent(options.q); //字符分割console.log(scan_url); 
      //打印出url//分割网址，提取出参数
      var surls = scan_url.split("?")
      var vid = surls[1]
      liveId = vid.split("&")[0];
      playF = vid.split("&")[1];
      playF = playF.replace("FLVAUTHKEY", "?");
      console.log('===playF===' + playF);
      this.setData({
        liveId: liveId,
        playF: playF
      })
    }
    if (options != null && options.playF != null) {
      console.log(options.playF);
      playF = options.playF.replace("FLVAUTHKEY", "?");
      liveId = options.liveId;
      console.log(playF);
      this.setData({
        liveId: liveId,
        playF: playF
      })
    }
    var that = this;
    wx.request({
      url: 'https://www.zuoye123.com/i_cloudmate/livepublicplay/wechat_public_play',
      data: {
        liveId: liveId
      },
      success:function(res){
        if (res.data.errorCode ='10030'){
          wx.showModal({
            title: '提示',
            content: '课程未开始',
            showCancel:false
          })

        } else if (res.data.errorCode =='10002'){
          wx.showModal({
            title: '提示',
            content: '参数错误',
            showCancel: false
          })
        }else{
          
        }
      }
    })


    console.log('=====onLoad end =============');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    var self = this;
    this.data.videoContext = wx.createLivePlayerContext(app.globalData.currentlive.liveId);
    console.log(this.data.videoContext);

    if (this.data.playF == '') {
      console.log('ready设置播放配置！！！！');

      console.log('===========app=========')
      console.log(app)
    }

    //默认进入直播全屏
    if (!this.data.fullScreen) {
      this.data.videoContext.requestFullScreen({
        // direction: 90,
      })
    } else { }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setKeepScreenOn({
      keepScreenOn: true,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    console.log("==========onShareAppMessage===============")
    console.log(this.data.playF);
    console.log(this.data.userId);
    console.log(this.data.liveId);
    var playF = this.data.playF;
    playF = playF.replace("?", "FLVAUTHKEY");
    console.log('playf=' + playF);
    console.log(res);
    return {
      title: '分享课堂',
      path: '/pages/publiclive/publiclive?userId=-1&liveId=' + this.data.liveId + '&playF=' + playF,
      success: function (res) {
        console.log(res);
      }
    }
  },

  onFullScreenChange: function (e) {
    this.setData({
      fullScreen: e.detail.fullScreen
    })
    console.log(e);
    wx.showToast({
      title: this.data.fullScreen ? '全屏' : '退出全屏',
    })
  },
})